$(document).ready(function(){
    var code="";//邮箱验证码
    var result1="true";//验证用户名重复
    var result2="true";//验证密码是否一致
    var result3="true";//验证邮箱验证码是否正确
    var result4="true";//验证用户信息是否填写完毕
    $.ajax({
        type : "get",
        url : "/register/getClazz",
        dataType : "json",
        success : function(data) {
            for(var i in data){
                if(i==0){
                    $("#selectClazz").append("<option value="+data[i].id+" selected='selected'>"+data[i].name+"</option>");
                }else{
                    $("#selectClazz").append("<option value="+data[i].id+">"+data[i].name+"</option>");
                }
            }
            console.log(data);
        },
        error : function(xhr,textStatus) {
            console.log('错误');
            console.log(xhr);
            console.log(textStatus);
        }
    });
    $("#userName").blur(function (){
        var userName=$("#userName").val();
        $.ajax({
            type : "GET",
            url : "/register/checkUserName",
            dataType : "text",
            data : {
                name:userName
            },
            success : function(data) {
                if(userName==data){
                    result1="false";
                    $("#nameHide").show();
                }else{
                    result1="true";
                    $("#nameHide").hide();
                }
                console.log(data);
            },
            error : function(xhr,textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });
    });

    $("#confirmPassword").blur(function(){
        var password=$("#password").val();
        var confrimPassword=$("#confirmPassword").val();
        if(password!=confrimPassword){
            result2="false";
            $("#confirmPasswordHide").show();
        }else{
            result2="true"
            $("#confirmPasswordHide").hide();
        }
    });

    $("#confirmEmail").click(function(){
        alert("邮箱验证码已经发送");
        var userName=$("#userName").val();
        var email=$("#email").val();
        $.ajax({
            type : "GET",
            url : "/register/checkEmail",
            dataType : "text",
            data : {
                email:email,
                name:userName
            },
            success : function(data) {
                code=data;
                console.log(data);
            },
            error : function(xhr,textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });
    });

    $("#emailConfirm").blur(function(){
       var emailConfirm=$("#emailConfirm").val();
        if(emailConfirm!=code){
            result3="false";
            alert("邮箱验证码填写错误！");
        }else{
            result3="true";
        }
    });

    $("#register").click(function(){
        var userName=$("#userName").val();
        var password=$("#password").val();
        var confirmPassword=$("#confirmPassword").val();
        var email=$("#email").val();
        var emailConfirm=$("#emailConfirm").val();
        var clazzId=$("#selectClazz").val();
        if(userName==""||password==""||confirmPassword==""||email==""||emailConfirm==""||
        userName==null||password==null||confirmPassword==null||email==null||emailConfirm==null){
            $("#registerHide").show();
            result4="false";
        }else {
            result4="true";
            $("#registerHide").hide();
        }



        if(result1=="true"&&result2=="true"&&result3=="true"&&result4=="true"){
            $("#registerHide").hide();
            $.ajax({
                type : "POST",
                url : "/register/addUser",
                dataType : "text",
                data : {
                    name:userName,
                    password:password,
                    email:email,
                    clazzId:clazzId
                },
                success : function(data) {
                    if(1==data){
                        alert("注册成功");
                        window.location.href="login.html";
                    }
                    console.log(data);
                },
                error : function(xhr,textStatus) {
                    console.log('错误');
                    console.log(xhr);
                    console.log(textStatus);
                }
            });
        }
    })
});